package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.User;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author FXB
 * @createTime 2021年11月15日 14:22:00
 */

/**
 * 菜单管理
 */
@RestController
@RequestMapping(path = "/menu")
@CrossOrigin
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;



    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping
    public Result findAll() {

        try {
            List<Menu> menus = menuService.findAll();
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menus);
        } catch (Exception e) {
            log.error("查询所有菜单失败！",e);
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }

    }


    /**
     * 查询分页所有菜单
     * @return
     */
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean){

        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
           log.info("获取当前菜单成功");
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            log.error("查询所有菜单失败！",e);
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }
    }


    /**
     * 删除菜单组信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {

        //查询该id下面的子菜单对应的ID
        List<Integer> menuIdList = menuService.findParentMenuIdById(id);
        //把要删除的id加到删除的集合
        menuIdList.add(id);
        try {
            Boolean result = menuService.deleteById(menuIdList);
            if(result){
                log.info("删除id{},成功",id);
                return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
            }
            log.info("删除菜单失败,，存在关联表");
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        } catch (Exception e) {
            log.error("删除菜单失败",e);
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }


    /**
     * 根据id查询菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }


    /**
     * 查询检查项关联的检查组数据
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}/permissions")
    public Result selectedCheckGroupIds(@PathVariable("id") Integer id) {
        try {
            Integer[] permissionsIds = menuService.selectPermissionsIds(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, permissionsIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    /**
     * 确定编辑, 更新数据
     * @param menu
     * @param permissionIds
     * @return
     */
    @PutMapping
    public Result edit(@RequestBody Menu menu, @RequestParam("permissionIds") List<Integer> permissionIds) {

        try {
            //修改菜单表
           menuService.edit(menu);
            //修改权限菜单表
            menuService.updateMenuPermissions(permissionIds,menu);

            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }


    /**
     * 新增菜单项
     *
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Menu menu, @RequestParam("permissionIds") List<Integer> permissionIds) {
        try {

            menuService.add(menu,permissionIds);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }



    /**
     * 动态获取菜单
     * @return
     */
    @GetMapping(path = "/getUserMenu")
    public Result findUserMenu(){

      //  User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      // String username = user.getUsername();
        //todo  测试
        String username="admin";
        try {
            Set<Menu> menuByUsername = menuService.findMenuByUsername(username);

            log.info("获取当前用户菜单成功");
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuByUsername);
        } catch (Exception e) {
            log.error("查询当前用户菜单失败！",e);
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }
    }


}
